package com.cyms.modules.order.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.cyms.common.annotation.SysLog;
import com.cyms.modules.sys.controller.AbstractController;
import com.cyms.modules.sys.entity.SysUserEntity;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.base.entity.LineEntity;
import com.cyms.modules.base.entity.LineScheduleEntity;
import com.cyms.modules.base.entity.LineTypeEntity;
import com.cyms.modules.base.service.LineService;
import com.cyms.modules.base.service.LineTypeService;
import com.cyms.modules.order.entity.OrderAmountEntity;
import com.cyms.modules.order.entity.OrderEntity;
import com.cyms.modules.order.entity.OrderScheduleEntity;
import com.cyms.modules.order.service.OrderAmountService;
import com.cyms.modules.order.service.OrderScheduleService;
import com.cyms.modules.order.service.OrderService;

import io.jsonwebtoken.lang.Collections;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.common.utils.DateUtils;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;

/**
 * 订单
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-13 22:59:47
 */
@RestController
@RequestMapping("order/order")
public class OrderController extends AbstractController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private OrderScheduleService orderScheduleService;
	@Autowired
	private OrderAmountService orderAmountService;
	@Autowired
    private LineTypeService lineTypeService;
	@Autowired
    private LineService lineService;
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("order:order:list")
	public R list(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		PageUtils page = orderService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/amountlist")
	@RequiresPermissions("order:order:list")
	public R amountlist(@RequestParam Map<String, Object> params) {
		params.put("createOrg",getUser().getCompanyId());
		PageUtils page = orderService.queryAmountPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("order:order:info")
	public R info(@PathVariable("id") Long id) {
		OrderEntity order = orderService.selectById(id);
		List<OrderScheduleEntity> scheduleList = orderScheduleService
				.selectList(new EntityWrapper<OrderScheduleEntity>().eq("order_id", id));
		order.setScheduleList(scheduleList);
		//
		List<OrderAmountEntity> amountList = orderAmountService
				.selectList(new EntityWrapper<OrderAmountEntity>().eq("order_id", id));
		order.setAmountList(amountList);
		//
		LineTypeEntity lineType = lineTypeService.selectById(order.getLineTypeId());
		order.setLineType(lineType);
		return R.ok().put("order", order);
	}

	/**
	 * 保存
	 */
	@SysLog("新增订单信息")
	@RequestMapping("/save")
	@RequiresPermissions("order:order:save")
	public R save(@RequestBody OrderEntity order) {
		order.setCreateDate(new Date());
		order.setCreateDept(getUser().getDeptId());
		order.setCreateOrg(getUser().getCompanyId());
		order.setCreateUser(getUserId());
		order.setStatus("1");
		String orderNo = DateUtils.format(new Date(), "yyyyMMddHHmmss");
		order.setOrderNo(orderNo);
		//
		order.setTotalAmount(order.getAlreadyAmount().add(order.getRestAmount()));
		orderService.insert(order);

		for (OrderScheduleEntity orderScheudle : order.getScheduleList()) {
			orderScheudle.setOrderId(order.getId());
			orderScheduleService.insertOrUpdate(orderScheudle);
		}
		//

		for (OrderAmountEntity orderAmount : order.getAmountList()) {
			orderAmount.setOrderId(order.getId());
			orderAmountService.insertOrUpdate(orderAmount);
		}
		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改订单信息")
	@RequestMapping("/update")
	@RequiresPermissions("order:order:update")
	public R update(@RequestBody OrderEntity order) {
		order.setTotalAmount(order.getAlreadyAmount().add(order.getRestAmount()));
		orderService.updateById(order);
		List<OrderScheduleEntity> scheduleList = orderScheduleService
				.selectList(new EntityWrapper<OrderScheduleEntity>().eq("order_id", order.getId()));
		if (scheduleList.size() > 0) {
			orderScheduleService.deleteBatchIds(
					scheduleList.parallelStream().map(scheudle -> scheudle.getId()).collect(Collectors.toList()));
		}
		for (OrderScheduleEntity orderScheudle : order.getScheduleList()) {
			orderScheudle.setOrderId(order.getId());
			orderScheduleService.insertOrUpdate(orderScheudle);
		}
		//
		List<OrderAmountEntity> amountList = orderAmountService
				.selectList(new EntityWrapper<OrderAmountEntity>().eq("order_id", order.getId()));
		if (amountList.size() > 0) {
			orderAmountService.deleteBatchIds(
					amountList.parallelStream().map(amount -> amount.getId()).collect(Collectors.toList()));
		}
		for (OrderAmountEntity orderAmount : order.getAmountList()) {
			orderAmount.setOrderId(order.getId());
			orderAmountService.insertOrUpdate(orderAmount);
		}
		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("订单销账")
	@RequestMapping("/finishOrder")
	@RequiresPermissions("order:order:update")
	public R finishOrder(@RequestBody OrderEntity order) {
		order.setTotalAmount(order.getAlreadyAmount().add(order.getRestAmount()));
		//
		order.setStatus("5");
		order.setFinishUser(getUser().getUsername());
		orderService.updateById(order);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("订单批量销账")
	@RequestMapping("/batchFinishOrder")
	@RequiresPermissions("order:order:update")
	public R batchFinishOrder(@RequestBody Long[] ids) {
		
		SysUserEntity user = getUser();
		orderService.batchFinishOrder(user,ids);

		return R.ok();
	}
	/**
	 * 删除
	 */
	@SysLog("删除订单")
	@RequestMapping("/delete")
	@RequiresPermissions("order:order:delete")
	public R delete(@RequestBody Long[] ids) {
		orderService.deleteBatchIds(Arrays.asList(ids));

		return R.ok();
	}

}
